package NewCode_150;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/10/27 20:42
 */
public class Solution {
    private boolean isOperator(String str){
        return str.equals("+")
                || str.equals("-")
                || str.equals("*")
                || str.equals("/");
    }
    private long cal (long num1,long num2,String op){
//        if (op.equals("+")) {
//            return num1+num2;
//        }
//        if (op.equals("-")) {
//            return num1-num2;
//        }
//        if (op.equals("*")) {
//            return num1*num2;
//        }
//        if (op.equals("/")) {
//            return num1/num2;
//        }
//        return -1;
        //
        switch (op){
            case "+" : return num1+num2;
            case "-" : return num1-num2;
            case "*" : return num1*num2;
            case "/" : return num1/num2;
            default: return -1;
        }
    }
    public int evalRPN(String[] tokens) {
        Deque<Long> stack = new LinkedList<>();
        for (String token : tokens){
            if (!isOperator(token)){
                stack.push(Long.parseLong(token));
            }else {
                long n2 = stack.pop();
                long n1 = stack.pop();
                //计算
                long res = cal(n1,n2,token);
                stack.push(res);
            }
        }
        long e = stack.pop();
        return (int) e;

    }
}
